package com.socket;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/29 9:52
 * @since JDK 1.8
 */
@Slf4j
public class SocketServer {

    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(6666);

        while (true) {
            log.info("scoket start!");
            Socket socket = server.accept();
            invoke(socket);
        }
    }

    private static void invoke(final Socket socket) throws IOException {
        new Thread(new Runnable() {
            public void run() {
                ObjectInputStream is = null;
                ObjectOutputStream os = null;
                try {
                    is = new ObjectInputStream(new BufferedInputStream(
                            socket.getInputStream()));
                    os = new ObjectOutputStream(socket.getOutputStream());
                    Object obj = is.readObject();
                    Map<String, Object> req = (Map<String, Object>) obj;  //传过来的
                    Map<String, Object> res=null;   //传过去的
                    //Method[] methods = req.getCls().getDeclaredMethods();
                    String cls=String.valueOf(req.get("class"));
                    String methodName=String.valueOf(req.get("methodName"));
                    log.info("cls: "+cls+"  methodName: "+methodName);
                    Class<?> clazz=Class.forName(cls);
                    Method[] methods=clazz.getDeclaredMethods();
                    Method invokeMethod=null;
                    for (int i = 0; i < methods.length; i++) {
                        Method methods1=methods[i];
                        if(!methods1.getName().equals(methodName)){
                            continue;
                        }
                        invokeMethod=methods1;
                    }
                    if(invokeMethod==null){
                        return;
                    }
                    invokeMethod.setAccessible(true);
                    Object value=invokeMethod.invoke(clazz.newInstance());
                    log.info("======"+value);
                    res=new HashMap<>();
                    res.put("body",value);
                    os.writeObject(res);
                    os.flush();
                }catch (InstantiationException e){
                    log.info(e.getMessage());
                }catch (InvocationTargetException e2){
                    log.info("目标调用异常");
                }catch (IllegalAccessException e){
                    log.info("这是");
                } catch (IOException ex) {
                    log.info("读取数据出错!");
                } catch (ClassNotFoundException ex) {
                    log.info("缺少类信息!");
                } finally {
                    try {
                        is.close();
                    } catch (Exception ex) {
                    }
                    try {
                        os.close();
                    } catch (Exception ex) {
                    }
                    try {
                        socket.close();
                    } catch (Exception ex) {
                    }
                }
            }
        }).start();
    }
}
